﻿using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
using Klear.Library.Models;
using Klear.Library.Services;
using Ursa.Controls;

namespace Klear.Library.ViewModels;

public class CategoryViewModel : ViewModelBase
{
    private string _categoryNameToEdit;
    private int _categoryToEditId;

    private ICategoryService _categoryService;
    
    public string CategoryNameToEdit
    {
        get => _categoryNameToEdit;
        set => SetProperty(ref _categoryNameToEdit, value);
    }

    public int CategoryToEditId
    {
       get => _categoryToEditId;
       set => SetProperty(ref _categoryToEditId, value);
    }

    public CategoryViewModel(ICategoryService categoryService) {
        ExpenseCategoryCollection = new ObservableCollection<CategorySingleViewModel>();
        IncomeCategoryCollection = new ObservableCollection<CategorySingleViewModel>();
        // // 添加几条默认数据
        // CategoryModel traffic = new CategoryModel(1, "交通",CategoryType.Expense);
        // CategoryModel food = new CategoryModel(2, "餐饮",CategoryType.Expense);
        // CategoryModel entertainment = new CategoryModel(3, "娱乐",CategoryType.Expense);
        // CategoryModel shopping = new CategoryModel(4, "购物",CategoryType.Expense);
        // CategoryModel daily = new CategoryModel(5, "日常",CategoryType.Expense);
        // CategorySingleViewModel trafficViewModel = 
        //     new CategorySingleViewModel(this) { Name = traffic.CategoryName, Category = traffic };
        // CategorySingleViewModel foodViewModel =
        //     new CategorySingleViewModel(this) { Name = food.CategoryName, Category = food };
        // CategorySingleViewModel entertainmentViewModel =
        //     new CategorySingleViewModel(this) { Name = entertainment.CategoryName, Category = entertainment };
        // CategorySingleViewModel shoppingViewModel =
        //     new CategorySingleViewModel(this) { Name = shopping.CategoryName, Category = shopping };
        // CategorySingleViewModel dailyViewModel =
        //     new CategorySingleViewModel(this) { Name = daily.CategoryName, Category = daily };
        // ExpenseCategoryCollection.Add(trafficViewModel);
        // ExpenseCategoryCollection.Add(foodViewModel);
        // ExpenseCategoryCollection.Add(entertainmentViewModel);
        // ExpenseCategoryCollection.Add(shoppingViewModel);
        // ExpenseCategoryCollection.Add(dailyViewModel);
        // // 添加几条默认数据
        // CategoryModel salary = new CategoryModel(6, "工资", CategoryType.Income);
        // CategorySingleViewModel salaryViewModel =
        //     new CategorySingleViewModel(this) { Name = salary.CategoryName, Category = salary };
        // IncomeCategoryCollection.Add(salaryViewModel);
        // CategoryModel stock = new CategoryModel(7, "股票", CategoryType.Income);
        // CategorySingleViewModel stockViewModel =
        //     new CategorySingleViewModel(this) { Name = stock.CategoryName, Category = stock };
        // IncomeCategoryCollection.Add(stockViewModel);
        
        _categoryService = categoryService;
        LoadCategories();
        
    }

    private async void LoadCategories() {
        // 清空分类数据
        ExpenseCategoryCollection.Clear();
        IncomeCategoryCollection.Clear();
        
        // 读取分类数据
        var expenseCategories = await _categoryService.GetExpenseCategoriesAsync();
        var incomeCategories = await _categoryService.GetIncomeCategoriesAsync();
        
        // 将分类数据转换为ViewModel并添加到集合中
        foreach (var category in expenseCategories) {
            ExpenseCategoryCollection.Add(new CategorySingleViewModel(this, _categoryService)
                { Name = category.CategoryName, Category = category });
        }
        foreach (var category in incomeCategories) {
            IncomeCategoryCollection.Add(new CategorySingleViewModel(this, _categoryService)
                { Name = category.CategoryName, Category = category });
        }
    }

    public static ObservableCollection<CategorySingleViewModel> ExpenseCategoryCollection { get; set; }
    
    public static ObservableCollection<CategorySingleViewModel> IncomeCategoryCollection { get; set; }

    public async void SaveAdd(string categoryName, CategoryType categoryType) {
        // 查询所有分类
        var categories = await _categoryService.GetCategoriesAsync();
        // 检查分类名称是否已存在
        foreach (var category in categories) {
            if (categoryName.Equals(category.CategoryName)) {
                // TODO:分类名称已存在，提示用户
                return;
            }
        }
        // TODO: 添加分类
        CategoryModel categoryModel = new CategoryModel();
        categoryModel.CategoryName = categoryName;
        categoryModel.CategoryType = categoryType;
        await _categoryService.AddCategoryAsync(categoryModel);
        LoadCategories();
    }

    public async void SaveEdit(string categoryName, CategoryType categoryType) {
        // 查询所有分类
        var categories = await _categoryService.GetCategoriesAsync();
        // 检查分类名称是否已存在
        foreach (var category in categories) {
            if (categoryName.Equals(category.CategoryName)) {
                // TODO:分类名称已存在，提示用户
                return;
            }
        }
        // var id = this.Category.CategoryId;
        CategoryModel categoryModel = new CategoryModel();
        categoryModel.CategoryId = _categoryToEditId;
        categoryModel.CategoryName = categoryName;
        categoryModel.CategoryType = categoryType;
        await _categoryService.UpdateCategoryAsync(categoryModel);
        LoadCategories();
    }

    public async void DoDelete(int id) {
        // CategoryModel categoryModel = new CategoryModel();
        // categoryModel.CategoryId = id;
        // categoryModel.CategoryName = categoryName;
        // categoryModel.CategoryType = categoryType;
        await _categoryService.DeleteCategoryAsync(id);
        LoadCategories();
    }


}

public class CategorySingleViewModel
{
    private readonly CategoryViewModel _categoryViewModel;
    private readonly ICategoryService _categoryService;

    public CategorySingleViewModel(CategoryViewModel categoryViewModel, ICategoryService categoryService) {
        _categoryViewModel = categoryViewModel;
        _categoryService = categoryService;
        EditCommand = new RelayCommand<string>(Edit);
        DeleteCommand = new RelayCommand(Delete);
    }

    // 分类的名称
    public string Name { get; set; }
    
    // 分类在数据库中的对象
    public CategoryModel Category;
    
    public IRelayCommand<string> EditCommand { get; set; }

    // 单纯用于数据回显和被修改分类id的赋值，真正的修改要由保存按钮触发
    private void Edit(string categoryNameToEdit) {
        _categoryViewModel.CategoryNameToEdit = categoryNameToEdit;
        _categoryViewModel.CategoryToEditId = Category.CategoryId;
    }
    
    public IRelayCommand DeleteCommand { get; set; }

    private void Delete() {
        // 单纯为要删除分类的id赋值
        _categoryViewModel.CategoryToEditId = Category.CategoryId;
    }
    
}